<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			.hot-city-item{
				cursor: pointer;
			}
		</style>
	</head>
	<body>
		<div id="city_panel" class="city-choose-panel" style="display: none;">
			<div class="panel">
				<div class="content poz-relative">
					<button class="close-btn" onclick="closeCityChoosePanel()"><span class="iconfont icon-close"></span></button>
					<div class="items-display-flex flex-row flex-between part1">
						<div class="lab1">选择城市</div>
						<div class="lab2">
							<span>热门城市</span>
							<span class="hot-city-item" v-for="city in hotcities" v-on:click="regionselect(city)">{{city.region_name}}</span>
						</div>
					</div>
					<div class="horizon"></div>
					<div class="items-display-flex flex-row flex-between part2">
						<div class="per-letter items-display-flex flex-row" v-for="item in allcities">
							<div class="lab2">{{item.flag}}</div>
							<div class="right">
								<span class="lab3" v-for="city in item.cities"  v-on:click="regionselect(city)">{{city.region_name}}</span>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
	<script src="../js/pinyin.js"></script>
	<script>
		

		var condition;
		
		var AllObj = {id:0,region_name:'不限',child:[]};
		
		document.getElementById('city_panel').style.display = 'none';
		var cityChoosePanel = new Vue({
			el:'#city_panel',
			data:{
				hotcities:[],
				allcities:[]				
			},
			methods: {
				regionselect:function(city){
					if(maincnt){
						maincnt.sregion = city.id;						
					}
					
					if(footCnt){
						footCnt.curcity = city;
					}
					closeCityChoosePanel();
					
					setCookie('kdcurcityId',city.id);
					setCookie('kdcurcityName',city.region_name);
					if(city.region_name == '海外置业'){
						window.location.href = 'hwzy.html';
					}
					else{
						if(typeof pagename == "string" && pagename == 'hw'){
							window.location.href = 'qgxf.html';
						}
						else{
							if(typeof initMenu === "function"){
								initMenu();
							}
							
							if(typeof loadHouseList === "function"){
								loadHouseList();
							}
						}
					}
					
				}
			}
			
		});
		document.getElementById('city_panel').style.height = document.body.clientHeight + 'px';
		$(document).ready(function(){
			loadBaseDb();
		});
		function closeCityChoosePanel(){
			document.getElementById('city_panel').style.display = 'none';
		}
		function showCityChoosePanel(){
			document.getElementById('city_panel').style.display = 'inherit';
		}
		function checkCurrentCity(){
			
			var curcityId = getCookie("kdcurcityId");
			console.log('curcityId='+curcityId);
//			console.log(returnCitySN);
			if(!curcityId || curcityId<=0){
				if(returnCitySN!=null && returnCitySN){
					var cityName = returnCitySN['cname'];
					for(var i=0;i<condition.regions.length;i++){
						if(cityName.indexOf(condition.regions[i].region_name)>=0){
							console.log(condition.regions[i].region_name);
							console.log(condition.regions[i].id)
							setCookie('kdcurcityId', condition.regions[i].id);
							setCookie('kdcurcityName', condition.regions[i].region_name);
							break;
						}
					}
				}
			}
			
			if(typeof reloadMenu === 'function'){
				reloadMenu();
			}
		}
		function getRegionName(rid){
			var rName = '';
			for(var i=0;i<condition.regions.length;i++){
				if(condition.regions[i].id == rid){
					rName = condition.regions[i].region_name;
					break;
				}
				else{
					
					if(!condition.regions[i] || condition.regions[i].length<=0 || !condition.regions[i].child)
						continue;
					
					for(var j=0;j<condition.regions[i].child.length;j++){
						if(rid == condition.regions[i].child[j].id){
							rName = condition.regions[i].region_name + ' ' + condition.regions[i].child[j].region_name;
							break;
						}
					}
				}
			}
			return rName;
		}
		
		function getRegionDb(rid){
			for(var i=0;i<condition.regions.length;i++){
				if(condition.regions[i].id == rid){
					return {cur:condition.regions[i],par:null};
				}
				else{
					
					if(!condition.regions[i] || condition.regions[i].length<=0 || !condition.regions[i].child)
						continue;
					
					for(var j=0;j<condition.regions[i].child.length;j++){
						if(rid == condition.regions[i].child[j].id){
							return {cur:condition.regions[i].child[j],par:condition.regions[i]};
						}
					}
				}
			}
			return {cur:null,par:null};
		}
		
		function getRegionChild(rid){
			var region = null;
			for(var i=0;i<condition.regions.length;i++){
				if(condition.regions[i].id == rid){
					region = condition.regions[i];
					break;
				}
			}
			if(region == null){
				region = AllObj;
			}
			return region;
		}
		
		function getPrices(){
			var prices = condition.prices;
			var tmp = [
				{
					id:0,
					name:'不限'
				}
			];
			for(var i=0;i<prices.length;i++){
				var price = {
					id:prices[i].id,
					name:prices[i].text
				}
				tmp.push(price);
			}
			return tmp;
		}
		
		function getRentPrices(){
			var prices = condition.rentprices;
			var tmp = [
				{
					id:0,
					name:'不限'
				}
			];
			for(var i=0;i<prices.length;i++){
				var price = {
					id:prices[i].id,
					name:prices[i].text
				}
				tmp.push(price);
			}
			return tmp;
		}
		
		function getHouseType(){
			var htypes = condition.houseType;
			var tmp = [
				{
					id:0,
					name:'不限',
					number:0
				}
			];
			for(var i=0;i<htypes.length;i++){
				var htype = {
					id:htypes[i].id,
					name:htypes[i].house_type,
					number:htypes[i].number
				}
				tmp.push(htype);
			}
			return tmp;
		}
		
		function getCategory(){
			var categorys = condition.category;
			var tmp = [
				{
					id:0,
					name:'不限'
				}
			];
			for(var i=0;i<categorys.length;i++){
				var category = {
					id:categorys[i].id,
					name:categorys[i].category
				}
				tmp.push(category);
			}
			return tmp;
		}
		
		function loadBaseDb(){
			
			cityChoosePanel.hotcities = [AllObj,{region_name:'北京',id:130,level:1},
			{region_name:'上海',id:1,level:1}];
			
			loadCityData();
			var urlstr = publicUrl+'/v1/basic-data/pc-base';
			jQuery.get(urlstr,{},function(res){
				condition = res.data;
				var regions = res.data.regions;
				
				for(var i=0;i<condition.regions.length;i++){				
					condition.regions[i].level = 0;
					if(!condition.regions[i] || condition.regions[i].length<=0 || !condition.regions[i].child)
						continue;
					for(var k = 0;k<condition.regions[i].child.length;k++){
						condition.regions[i].child[k].level = 1;
					}
				}
				
				if(typeof initMenu === "function"){
					initMenu();
				}
				if(typeof loadHouseList === "function"){
					loadHouseList();
				}
				if(maincnt){
					maincnt.pricesArr = res.data.prices;
					maincnt.houseTypeArr = res.data.houseType;
					maincnt.categoryArr = res.data.category;
					maincnt.RentpricesArr = res.data.rentprices;
				}
				
				var footerArr = [];
				//处理首字符拼音
				for(var i=0;i<regions.length;i++){
					
					var FirstStr = getPinYinFirstCharacter(regions[i].region_name, "-", true);
					SaveCityDb(FirstStr,regions[i]);
					footerArr.push(regions[i]);
				}
				
				for(var i=0;i<cityChoosePanel.allcities.length;i++){
					if(cityChoosePanel.allcities[i].cities.length<=0){
						cityChoosePanel.allcities.splice(i,1);
						i--;
					}
				}
				
				if(cityCnt){
					cityCnt.allcity = footerArr;
				}
				
				condition.regions.unshift(AllObj);
				
				checkCurrentCity();
			});
		}
		
		
		function SaveCityDb(FirstStr,regionsbd){
			var isHaveCity = false;
			
			if(FirstStr.length>1){
				FirstStr = FirstStr.substring(0,1);
			}
			
			for(var j = 0;j<cityChoosePanel.allcities.length;j++){			
				if(cityChoosePanel.allcities[j].flag == FirstStr){
					isHaveCity = true;							
					cityChoosePanel.allcities[j].cities.push(regionsbd);
					break;
				}
			}
			
			if(!isHaveCity){
				cityChoosePanel.allcities.push({flag:FirstStr,cities:[]});
				cityChoosePanel.allcities[cityChoosePanel.allcities.length-1].cities.push(regionsbd);
			}
		}
		
		function loadCityData(){
			
			var ZmArr = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
			
			cityChoosePanel.allcities = [];
			for(var i=0;i<ZmArr.length;i++){
				cityChoosePanel.allcities.push({flag:ZmArr[i],cities:[]})
			}
		}
		
		//海外
		function HwChange(){
			
		}
	</script>
</html>
